package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.MediaService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;

/**
 *音视频管理
 */
@RestController
@RequestMapping("/bk")
public class MediaController {

    private static final Logger logger = LogManager.getLogger(MediaController.class);

    @Resource
    private MediaService mediaService;

    @LoginRequired
    @PostMapping("/addMedia")
    @ApiOperation(value = "新增音频视频文件")
    public JsonResult addMedia(
            @ApiParam(value = "标题", required = true) @RequestParam(value = "title") String title,
            @ApiParam(value = "来源") @RequestParam(value = "origin",required = false) String origin,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "附件原名称") @RequestParam(value = "originName",required = false) String originName,
            @ApiParam(value = "附件URL") @RequestParam(value = "url",required = false) String url
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("title", title);
            pageData.put("url", url);
            pageData.put("origin", origin);
            pageData.put("content", content);
            pageData.put("originName", originName);

            mediaService.addMedia(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateMedia")
    @ApiOperation(value = "更新音频视频文件")
    public JsonResult updateMedia(
            @ApiParam(value = "ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "标题", required = true) @RequestParam(value = "title") String title,
            @ApiParam(value = "来源") @RequestParam(value = "origin",required = false) String origin,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "附件原名称") @RequestParam(value = "originName",required = false) String originName,
            @ApiParam(value = "附件URL") @RequestParam(value = "url",required = false) String url
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("title", title);
            pageData.put("url", url);
            pageData.put("origin", origin);
            pageData.put("content", content);
            pageData.put("originName", originName);

            mediaService.updateMedia(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteMedia")
    @ApiOperation(value = "删除音频视频文件")
    public JsonResult deleteMedia(
            @ApiParam(value = "音频视频文件ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            mediaService.deleteMedia(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/disableOrEnableMedia")
    @ApiOperation(value = "启用或者禁用音频视频文件")
    public JsonResult disableOrEnableMedia(
            @ApiParam(value = "音频视频文件ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            mediaService.disableOrEnableMedia(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listMedia")
    @ApiOperation(value = "获取音频视频音频视频文件列表")
    public JsonResult listMedia(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(mediaService.listMedia(pageData));
            result.setResultNum(mediaService.listMediaCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }




}
